package com.app.workmanagermodule

import android.os.Bundle
import android.util.Log
import androidx.work.Constraints
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.app.common.activity.BaseActivity
import com.app.workmanagermodule.worker.UploadWorker
import kotlinx.android.synthetic.main.activity_work_manager.*
import java.util.concurrent.TimeUnit

@Route(path = "/work_manager/work_manager_activity")
class WorkManagerActivity : BaseActivity() {

    val TAG = WorkManagerActivity::class.java.simpleName

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_work_manager)

        activity_work_manager_upload.setOnClickListener {

            val constraints = Constraints.Builder()
                    .setRequiresDeviceIdle(true)
                    .setRequiresCharging(true)
                    .build()

            val onTimeWorkRequestBuilder = OneTimeWorkRequestBuilder<UploadWorker>()
                    //.build()
            onTimeWorkRequestBuilder.setConstraints(constraints)
            onTimeWorkRequestBuilder.setInitialDelay(1, TimeUnit.MINUTES)
            val uploadWorkRequest = onTimeWorkRequestBuilder.build()

            Log.d(TAG, "uploadWorkRequestId ---> ${uploadWorkRequest.id}")
            WorkManager.getInstance(this@WorkManagerActivity).enqueue(uploadWorkRequest)
            //WorkManager.getInstance(this@WorkManagerActivity).getWorkInfosByTagLiveData()
        }

    }

}